﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using InfoExpress.WinApp.Utils;
using InfoExpress.HIV.WinApp.Utils;
using InfoExpress.HIV.Domains;

namespace InfoExpress.HIV.WinApp.Business
{
  public static class BenhNhanUtils
  {
    /// <summary>
    /// Return GHIJ
    /// </summary>
    /// <param name="maDonVi"></param>
    /// <param name="maPhanLoai"></param>
    /// <param name="nam"></param>
    /// <returns></returns>
    public static string TaoSttBenhNhan(string maDonVi, string maPhanLoai, string nam)
    {
      int result = 0;

      try
      {
        DataSet dtsResult = 
          GlobalCache.BenhNhanDangKyService.LaySttToiDa(maDonVi, int.Parse(maPhanLoai), nam);
        result = Convert.ToInt32(dtsResult.Tables[0].Rows[0][0]);
      }
      catch (Exception ex)
      {
        Logger.LogCritical("BenhNhanUtils::TaoSttBenhNhan", ex, "General", null);
        throw;
      }

      result++;
      int iMaPhanLoai = int.Parse(maPhanLoai);
      if ((iMaPhanLoai == 1) && result < 1000) 
        result += 1000;
      else if ((iMaPhanLoai == 9) && result < 9000) 
        result += 9000;
      else if ((iMaPhanLoai == 5) && result < 5000) 
        result += 5000;
      else if ((iMaPhanLoai == 6) && result < 6000) 
        result += 6000;
      else if ((iMaPhanLoai == 7) && result < 7000) 
        result += 7000;
      else if ((iMaPhanLoai == 8) && result < 8000) 
        result += 8000;
      else if ((iMaPhanLoai >= 2) && (iMaPhanLoai < 5) && result < 2000) 
        result += 2000;			

      return result.ToString();
    }

    /// <summary>
    /// Kiem tra ma benh nhan da ton tai.
    /// </summary>
    /// <param name="maBenhNhan"></param>
    /// <returns></returns>
    public static bool KiemTraMaBenhNhanTonTai(string maBenhNhan)
    {
      int total = 0;
      try
      {
        GlobalCache.BenhNhanDangKyService.GetPaged(String.Format("MaBenhNhan='{0}' AND DeleteFlag=0", maBenhNhan), string.Empty, 0, 1, out total);
      }
      catch(Exception ex)
      {
        Logger.LogCritical("BenhNhanUtils::KiemTraMaBenhNhanTonTai", ex, "General", null);
        throw;
      }

      return (total > 0);
    }

    /// <summary>
    /// Lay benh nhan Id tu ma benh nhan.
    /// </summary>
    /// <param name="maBenhNhan"></param>
    /// <returns></returns>
    public static int LayBenhNhanId(string maBenhNhan)
    {
      int total = 0;
      int benhNhanId = 0;
      try
      {
        TList<BenhNhanDangKy> benhNhanList =
          GlobalCache.BenhNhanDangKyService.GetPaged(String.Format("MaBenhNhan='{0}' AND DeleteFlag=0", maBenhNhan), string.Empty, 0, 1, out total);

        if(total > 0)
          benhNhanId = benhNhanList[0].BenhNhanId;
      }
      catch (Exception ex)
      {
        Logger.LogCritical("BenhNhanUtils::LayBenhNhanId", ex, "General", null);
        throw;
      }

      return benhNhanId;
    }

    public enum TrangThai
    {
      ChuaDuyet = 1,
      Duyet = 2,
      KhongDuyet = 3
    }

    public enum ChamDutDieuTri
    {
      TuVong = 0,
      MatHut = 1,
      ChuyenOpc = 2,
      ChoChuyenOpc = 3
    }
  }
}
